package com.lyj.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyj.mall.dao.ProductMapper;
import com.lyj.mall.enums.ResponseEnum;
import com.lyj.mall.pojo.Product;
import com.lyj.mall.service.CategoryService;
import com.lyj.mall.service.ProductService;
import com.lyj.mall.vo.ProductDetailVo;
import com.lyj.mall.vo.ProductVo;
import com.lyj.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.lyj.mall.enums.ProductStatusEnum.DELETED;
import static com.lyj.mall.enums.ProductStatusEnum.OFF_SELE;

/**
 * @author lyj
 * @date 2020/1/7 23:10
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryService categoryService;

    @Override
    public ResponseVo<IPage> list(Integer categoryId, Integer pageNum, Integer pageSize) {
        Set categoryIdSet = new HashSet();
        if (null != categoryId) {
            categoryIdSet = categoryService.findSubCategoryId(categoryId, new HashSet<Integer>());
            categoryIdSet.add(categoryId);
        }
        Page page = new Page(pageNum, pageSize);

        IPage<Product> iPage = productMapper.selectByCategoryIdSet(page, categoryIdSet);
        List<Product> productList = iPage.getRecords();
        productList.stream().map(product -> {
            ProductVo productVo = new ProductVo();
            BeanUtil.copyProperties(product, productVo);
            return productVo;
        }).collect(Collectors.toList());
        return ResponseVo.success(iPage);
    }

    @Override
    public ResponseVo<ProductDetailVo> detail(Integer productId) {
        Product product = productMapper.selectById(productId);
        if (null == product) {
            return ResponseVo.error(ResponseEnum.PRODUCT_NOT_FIND);
        }
        if (product.getStatus().equals(OFF_SELE.getStatus()) || product.getStatus().equals(DELETED.getStatus())) {
            return ResponseVo.error(ResponseEnum.PRODUCT_OFF_SALE_OR_DELETED);
        }
        ProductDetailVo productDetailVo = product2ProductVo(product);
        return ResponseVo.success(productDetailVo);
    }

    @Override
    public List<Product> selectByProductIdSet(Set<Integer> productIdSet) {
        return productMapper.selectByProductIdSet(productIdSet);
    }

    private ProductDetailVo product2ProductVo(Product product) {
        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtil.copyProperties(product, productDetailVo);
        // 对库存敏感处理
        productDetailVo.setStock(productDetailVo.getStock() > 100 ? 100 : productDetailVo.getStock());
        return productDetailVo;
    }

}
